import apiClient from '@/api/interceptors';

/**
 * 获取所有作业
 */
export async function getAllJobs(clusterId) {
    try {
        const response = await apiClient.get(`/flink/${clusterId}/jobs`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取所有作业失败');
        }
    } catch (error) {
        console.error('请求所有作业失败:', error);
        throw error;
    }
}

/**
 * 获取运行中的作业
 */
export async function getRunningJobs(clusterId) {
    try {
        const response = await apiClient.get(`/flink/${clusterId}/jobs/running`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取运行中作业失败');
        }
    } catch (error) {
        console.error('请求运行中作业失败:', error);
        throw error;
    }
}

/**
 * 获取已完成的作业
 */
export async function getFinishedJobs(clusterId) {
    try {
        const response = await apiClient.get(`/flink/${clusterId}/jobs/finished`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取已完成作业失败');
        }
    } catch (error) {
        console.error('请求已完成作业失败:', error);
        throw error;
    }
}

/**
 * 获取作业详情
 */
export async function getJobInfo(clusterId, jobId) {
    try {
        const response = await apiClient.get(`/flink/${clusterId}/jobs/${jobId}`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取作业详情失败');
        }
    } catch (error) {
        console.error('请求作业详情失败:', error);
        throw error;
    }
}

/**
 * 获取已上传的JAR包列表
 */
export async function listJars(clusterId) {
    try {
        const response = await apiClient.get(`/flink/${clusterId}/jars`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取JAR包列表失败');
        }
    } catch (error) {
        console.error('请求JAR包列表失败:', error);
        throw error;
    }
}

/**
 * 上传JAR包
 */
export async function uploadJar(clusterId, file) {
    try {
        const formData = new FormData();
        formData.append('file', file);
        const response = await apiClient.post(`/flink/${clusterId}/jars/upload`, formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '上传JAR包失败');
        }
    } catch (error) {
        console.error('上传JAR包失败:', error);
        throw error;
    }
}

/**
 * 删除JAR包
 */
export async function deleteJar(clusterId, jarId) {
    try {
        const response = await apiClient.delete(`/flink/${clusterId}/jars/${jarId}`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '删除JAR包失败');
        }
    } catch (error) {
        console.error('删除JAR包失败:', error);
        throw error;
    }
}

/**
 * 提交作业
 */
export async function runJob(clusterId, data) {
    try {
        const response = await apiClient.post(`/flink/${clusterId}/jobs/run`, data);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '提交作业失败');
        }
    } catch (error) {
        console.error('提交作业失败:', error);
        throw error;
    }
}

/**
 * 取消作业
 */
export async function cancelJob(clusterId, jobId) {
    try {
        const response = await apiClient.post(`/flink/${clusterId}/jobs/${jobId}/cancel`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '取消作业失败');
        }
    } catch (error) {
        console.error('取消作业失败:', error);
        throw error;
    }
}

/**
 * 重启失败的作业
 */
export async function restartFailedJob(clusterId, jobId) {
    try {
        const response = await apiClient.post(`/flink/${clusterId}/jobs/${jobId}/restart`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '重启作业失败');
        }
    } catch (error) {
        console.error('重启作业失败:', error);
        throw error;
    }
} 